package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.dao.MaterialMapper;
import com.lucher.mes.po.Material;

@Controller
@RequestMapping("/material")  
public class MaterialController extends BaseController{
	@Autowired
	private MaterialMapper materialDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Material form) {
		return "material/findMaterialList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Material form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = materialDao.findByCount(form);
		List<Material> list = materialDao.findByListForPage(form);
		
		for (Material material : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", material.getId());
			objectMap.put("materialcode", material.getMaterialcode());
			objectMap.put("materialname", material.getMaterialname());
			objectMap.put("materialunit", material.getMaterialunit());
			objectMap.put("depotamount", material.getDepotamount());
			objectMap.put("materialstatus", material.getMaterialstatus());
			objectMap.put("remark", material.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Material form) {
		return "material/addMaterial";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Material form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getMaterialcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入材料编码");
			return responseJson;
		} else {
			Material oldMaterial = materialDao.findByMaterialcode(form.getMaterialcode());
			if (oldMaterial != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此材料编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getMaterialname())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入材料名称");
			return responseJson;
		} else {
			Material material = materialDao.findByMaterialname(form.getMaterialname());
			if (material != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此材料名称已经存在");
				return responseJson;
			}
		}
		
		//保存材料信息
		Integer materialId = materialDao.insertSelective(form);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Material form) {
		Material material = materialDao.selectByPrimaryKey(form.getId());
		form.setMaterial(material);

		return "material/updateMaterial";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Material form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getMaterialcode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，材料编码不能为空");
			return responseJson;
		} else {
			Material oldMaterial = materialDao.findByMaterialcode(form.getMaterialcode());
			if (oldMaterial != null && !oldMaterial.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，材料编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getMaterialname())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，材料名称不能为空");
			return responseJson;
		} else {
			Material oldMaterial = materialDao.findByMaterialname(form.getMaterialname());
			if (oldMaterial != null && !oldMaterial.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，材料名称已经存在");
				return responseJson;
			}
		}

		// 修改信息
		int flag = materialDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Material form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除材料
		materialDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Material> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Material material : formlist) {
			//删除材料
			materialDao.deleteByPrimaryKey(material.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
     * 获取材料的下拉列表框Json值
     */
	@ResponseBody //此标签表示返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	@RequestMapping(value="/getMaterialComboBoxJson")
	public List<Map<String,Object>> getMaterialComboBoxJson(Material form) {  
	    List<Material> materialList = materialDao.findByList(form);
	    List<Map<String, Object>> materialComboBoxJson = new ArrayList<Map<String, Object>>();
	    //添加请选择项-默认为空，查询所有
	    HashMap<String,Object> selectMap = new HashMap<String, Object>();
	    selectMap.put("id", "");
	    selectMap.put("text", "请选择");
	    materialComboBoxJson.add(selectMap);
	    
	    //封装区域属性结构信息
	    for(Material material:materialList){  
    	    HashMap<String,Object> materialMap = new HashMap<String, Object>();
    	    materialMap.put("id", material.getMaterialcode());
    	    materialMap.put("text", material.getMaterialname()+"("+material.getMaterialcode()+")");
    	    
    	    materialComboBoxJson.add(materialMap);  
	    }  
	   return materialComboBoxJson;
	}  
	
}
